package com.pds.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import com.pds.common.core.domain.R;
import com.pds.common.core.domain.entity.SysDept;
import com.pds.common.utils.DeptUtils;
import com.pds.system.service.ISysDeptService;
import com.pds.utils.RSAUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.pds.common.constant.Constants;
import com.pds.common.core.domain.AjaxResult;
import com.pds.common.core.domain.entity.SysMenu;
import com.pds.common.core.domain.entity.SysUser;
import com.pds.common.core.domain.model.LoginBody;
import com.pds.common.utils.SecurityUtils;
import com.pds.framework.web.service.SysLoginService;
import com.pds.framework.web.service.SysPermissionService;
import com.pds.system.service.ISysMenuService;

/**
 * 登录验证
 *
 * @author pds
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        String decryptUserName = null;
        String decryptPassword = null;
        try {
            decryptUserName = RSAUtil.decrypt(loginBody.getUsername(), RSAUtil.privateKay);
            decryptPassword = RSAUtil.decrypt(loginBody.getPassword(), RSAUtil.privateKay);
        } catch (Exception e) {
            e.printStackTrace();
        }
        loginBody.setUsername(decryptUserName);
        loginBody.setPassword(decryptPassword);
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid(),loginBody.getSource());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        SysDept dept = user.getDept();
        String secondLevelDeptId = DeptUtils.getSecondLevelDeptId(dept,3);
        SysDept sysDept = sysDeptService.selectDeptById(Long.parseLong(secondLevelDeptId));
        ajax.put("organizationId",sysDept.getOrganizationId());
        ajax.put("type",sysDept.getType());
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    @GetMapping("getSecretKey")
    public R getSecretKey(){
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("publicKey",RSAUtil.publicKey);
        hashMap.put("privateKey",RSAUtil.privateKay);
        return R.ok(hashMap);
    }
}
